<template>
  <div class="process-instance">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>流程实例列表</span>
        </div>
      </template>

      <!-- 搜索表单 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="流程名称">
          <el-input v-model="searchForm.processName" placeholder="请输入流程名称" />
        </el-form-item>
        <el-form-item label="业务Key">
          <el-input v-model="searchForm.businessKey" placeholder="请输入业务Key" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.suspended" placeholder="请选择状态" clearable>
            <el-option label="活动" :value="false" />
            <el-option label="挂起" :value="true" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>搜索
          </el-button>
          <el-button @click="resetSearch">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="实例ID" width="220" />
        <el-table-column prop="processDefinitionName" label="流程名称" />
        <el-table-column prop="businessKey" label="业务Key" />
        <el-table-column prop="startTime" label="开始时间" />
        <el-table-column prop="suspended" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.suspended ? 'warning' : 'success'">
              {{ scope.row.suspended ? '挂起' : '活动' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button-group>
              <el-button 
                v-if="scope.row.suspended" 
                type="primary" 
                size="small" 
                @click="handleActivate(scope.row)">
                激活
              </el-button>
              <el-button 
                v-else 
                type="warning" 
                size="small" 
                @click="handleSuspend(scope.row)">
                挂起
              </el-button>
              <el-button 
                type="info" 
                size="small" 
                @click="handleViewDetail(scope.row)">
                详情
              </el-button>
              <el-button 
                type="danger" 
                size="small" 
                @click="handleDelete(scope.row)">
                删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 流程实例详情对话框 -->
    <el-dialog v-model="detailDialogVisible" title="流程实例详情" width="800px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="实例ID">{{ currentInstance.id }}</el-descriptions-item>
        <el-descriptions-item label="流程定义">{{ currentInstance.processDefinitionName }}</el-descriptions-item>
        <el-descriptions-item label="业务Key">{{ currentInstance.businessKey }}</el-descriptions-item>
        <el-descriptions-item label="开始时间">{{ currentInstance.startTime }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="currentInstance.suspended ? 'warning' : 'success'">
            {{ currentInstance.suspended ? '挂起' : '活动' }}
          </el-tag>
        </el-descriptions-item>
      </el-descriptions>

      <div class="process-diagram" style="margin-top: 20px;">
        <div class="placeholder-diagram">流程图区域</div>
      </div>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { processInstanceApi } from '@/api/flowable'

// 搜索表单数据
const searchForm = ref({
  processName: '',
  businessKey: '',
  suspended: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页数据
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 详情对话框
const detailDialogVisible = ref(false)
const currentInstance = ref({})

// 获取流程实例列表
const getProcessInstanceList = async () => {
  loading.value = true
  try {
    const params = {
      ...searchForm.value,
      page: currentPage.value - 1,
      size: pageSize.value
    }
    const res = await processInstanceApi.getList(params)
    tableData.value = res.data.data
    total.value = res.data.total
  } catch (error) {
    console.error('获取流程实例列表失败', error)
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  getProcessInstanceList()
}

// 重置搜索
const resetSearch = () => {
  searchForm.value = {
    processName: '',
    businessKey: '',
    suspended: ''
  }
  handleSearch()
}

// 分页大小改变
const handleSizeChange = (val) => {
  pageSize.value = val
  getProcessInstanceList()
}

// 当前页改变
const handleCurrentChange = (val) => {
  currentPage.value = val
  getProcessInstanceList()
}

// 查看详情
const handleViewDetail = (row) => {
  currentInstance.value = row
  detailDialogVisible.value = true
}

// 激活流程实例
const handleActivate = (row) => {
  ElMessageBox.confirm('确认激活该流程实例吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      await processInstanceApi.activate(row.id)
      ElMessage.success('激活成功')
      getProcessInstanceList()
    } catch (error) {
      ElMessage.error('激活失败')
    }
  })
}

// 挂起流程实例
const handleSuspend = (row) => {
  ElMessageBox.confirm('确认挂起该流程实例吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      await processInstanceApi.suspend(row.id)
      ElMessage.success('挂起成功')
      getProcessInstanceList()
    } catch (error) {
      ElMessage.error('挂起失败')
    }
  })
}

// 删除流程实例
const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该流程实例吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      await processInstanceApi.delete(row.id)
      ElMessage.success('删除成功')
      getProcessInstanceList()
    } catch (error) {
      ElMessage.error('删除失败')
    }
  })
}

onMounted(() => {
  getProcessInstanceList()
})
</script>

<style scoped>
.process-instance {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.placeholder-diagram {
  height: 300px;
  border: 1px dashed #d9d9d9;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #909399;
}
</style> 